﻿// Decompiled with JetBrains decompiler
// Type: Commom.Util.TimeUtil
// Assembly: MT5Trade, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8045A2CB-DBD6-41AD-8804-F98CAC1BDC8D
// Assembly location: C:\projects\trade\trade_tool\trade0812\MT5Trade.dll

using System;

#nullable enable
namespace Commom.Util;

public static class TimeUtil
{
  private static DateTime? _dateTime;

  public static void SetTime(DateTime? dateTime) => TimeUtil._dateTime = dateTime;

  public static void SetTime(string dateTime)
  {
    TimeUtil._dateTime = ConvertUtil.ToDateOrNull((object) dateTime);
  }

  public static void Reset() => TimeUtil._dateTime = new DateTime?();

  public static DateTime GetDateTime()
  {
    return !TimeUtil._dateTime.HasValue ? DateTime.Now : TimeUtil._dateTime.Value;
  }

  public static DateTime GetDate() => TimeUtil.GetDateTime().Date;

  public static DateTime GetMonday(DateTime? date = null)
  {
    DateTime dateTime = date ?? DateTime.Now;
    return dateTime.AddDays((double) (1 - Convert.ToInt32(dateTime.DayOfWeek.ToString("d"))));
  }

  public static DateTime GetSunday(DateTime? date = null) => TimeUtil.GetMonday(date).AddDays(6.0);

  public static DateTime GetStartMonth(DateTime? date = null)
  {
    DateTime dateTime = date ?? DateTime.Now;
    return dateTime.AddDays((double) (1 - dateTime.Day));
  }

  public static DateTime GetEndMonth(DateTime? date = null)
  {
    DateTime dateTime = TimeUtil.GetStartMonth(date);
    dateTime = dateTime.AddMonths(1);
    return dateTime.AddDays(-1.0);
  }

  public static DateTime GetStartQuarter(DateTime? date = null)
  {
    DateTime dateTime = date ?? DateTime.Now;
    return dateTime.AddMonths(-((dateTime.Month - 1) % 3)).AddDays((double) (1 - dateTime.Day));
  }

  public static DateTime GetEndQuarter(DateTime? date = null)
  {
    DateTime dateTime = TimeUtil.GetStartQuarter(date);
    dateTime = dateTime.AddMonths(3);
    return dateTime.AddDays(-1.0);
  }

  public static long GetUnixTimestamp() => TimeUtil.GetUnixTimestamp(DateTime.Now);

  public static long GetUnixTimestamp(DateTime time)
  {
    DateTime dateTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local);
    return ConvertUtil.ToLong((object) ((time - dateTime.Add(new TimeSpan(8, 0, 0))).Ticks / 10000000L));
  }

  public static DateTime GetTimeFromUnixTimestamp(long timestamp)
  {
    return TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local).Add(new TimeSpan(long.Parse(timestamp.ToString() + "0000000"))).Add(new TimeSpan(8, 0, 0));
  }
}
